package com.qiongqi.datadir.service.impl;

import com.qiongqi.datadir.config.DataDirConfig;
import com.qiongqi.datadir.domain.Resource;
import com.qiongqi.datadir.mapper.ResourceMapper;
import com.qiongqi.datadir.service.IResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.List;

import static com.qiongqi.datadir.utils.uuid.IdUtils.fastSimpleUUID;
import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.ArrayUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

@Slf4j
@Service
@Transactional
public class ResourceServiceImpl implements IResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private DataDirConfig dataDirConfig;

    /**
     * 查询资源
     *
     * @param id 资源主键
     * @return 资源
     */
    @Override
    public Resource selectResourceById(String id) {
        return isNotBlank(id) ? resourceMapper.selectResourceById(id) : null;
    }

    /**
     * 查询资源列表
     *
     * @param resource 资源
     * @return 资源集合
     */
    @Override
    public List<Resource> selectResourceList(Resource resource) {
        if(nonNull(resource)){
            resource.setCreateBy(dataDirConfig.getName());
        }
        return nonNull(resource) ? resourceMapper.selectResourceList(resource) : List.of();
    }

    /**
     * 查询资源列表
     *
     * @param resource 资源
     * @return 资源集合
     */
    @Override
    public List<Resource> selectOtherAcconResourceList(Resource resource) {
        if(nonNull(resource)){
            resource.setCreateBy(dataDirConfig.getName());
        }
        return nonNull(resource) ? resourceMapper.selectOtherAcconResourceList(resource) : List.of();
    }

    /**
     * 新增资源
     *
     * @param resource 资源
     * @return 结果
     */
    @Override
    public int insertResource(Resource resource) {
        int ret = -1;
        if (nonNull(resource)) {
            Instant currentTime = Instant.now();
            resource.setId(fastSimpleUUID());
            resource.setCreateBy(dataDirConfig.getName());
            resource.setCreateTime(currentTime);
            resource.setUpdateBy(dataDirConfig.getName());
            resource.setUpdateTime(currentTime);
            ret = resourceMapper.insertResource(resource);
        }
        return ret;
    }

    /**
     * 修改资源
     *
     * @param resource 资源
     * @return 结果
     */
    @Override
    public int updateResource(Resource resource) {
        int ret = -1;
        if (nonNull(resource)) {
            resource.setUpdateBy(dataDirConfig.getName());
            resource.setUpdateTime(Instant.now());
            ret = resourceMapper.updateResource(resource);
        }
        return ret;
    }

    /**
     * 批量删除资源
     *
     * @param ids 需要删除的资源主键集合
     * @return 结果
     */
    @Override
    public int deleteResourceByIds(String[] ids) {
        return isNotEmpty(ids) ? resourceMapper.deleteResourceByIds(ids) : 0;
    }

    /**
     * 删除资源信息
     *
     * @param id 资源主键
     * @return 结果
     */
    @Override
    public int deleteResourceById(String id) {
        return isNotBlank(id) ? resourceMapper.deleteResourceById(id) : 0;
    }

    /**
     * 查询资源归属接入连接器列表
     *
     * @return 接入连接器名称列表
     */
    @Override
    public List<String> selectResourceOwners() {
        return resourceMapper.selectResourceOwners();
    }
}
